package com.cenntro.oss.controller;


import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.cenntro.common.util.NumUtil;
import com.cenntro.common.util.result.PageResult;
import com.cenntro.common.util.result.Result;
import com.cenntro.oss.model.dbo.DevSupplyImeiDO;
import com.cenntro.oss.model.dbo.SysAgentDO;
import com.cenntro.oss.model.qo.DeviceQO;
import com.cenntro.oss.model.vo.DeviceVO;
import com.cenntro.oss.service.DevDeviceOnlineService;
import com.cenntro.oss.service.DevSupplyImeiService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import io.swagger.annotations.Api;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2018-09-17
 */
@Api(value = "终端管理", tags = { "终端管理" })
@RestController
@RequestMapping("/imei")
public class DevSupplyImeiController {
	
	@Resource
	private DevSupplyImeiService supplyImeiService;
	@Resource
	private DevDeviceOnlineService deviceOnlineService;
	
	/**
	 * 
	* @Title selectPage
	* @Description 终端查询
	* @param dev
	* @return  
	* @throws
	 */
	@RequestMapping("/selectPage")
	public Result selectPage(DeviceVO dev){
		Map<String, Object> map = Maps.newHashMap();
		map.put("version", dev.getVersion());
		map.put("status", dev.getStatus());
		map.put("imei", dev.getImei());
		map.put("allotion", dev.getAllotion());
//		map.put("page", 1);
//		map.put("limit", 10);
//		map.put("offset", 0);
		
		Page<SysAgentDO> page = PageHelper.startPage(dev.getPage(), dev.getLimit(), true);
		List<Map<String, Object>> resList = supplyImeiService.selectByMap(map);
		List<DeviceQO> resultList = Lists.newArrayList();
		for (Map<String, Object> resultMap : resList){
			DeviceQO device = JSON.parseObject(JSON.toJSONString(resultMap), DeviceQO.class);
			device.setImeiId(NumUtil.toInt(resultMap.get("Id")));
			device.setOnlineId(NumUtil.toInt(resultMap.get("onlineId")));
			if(StringUtils.isNotBlank(device.getDeviceNo())){
				device.setSupply("波导");
			}else{
				device.setSupply("容大");
			}
			resultList.add(device);
		}
		return PageResult.ok(resultList, page.getTotal());
	}
	
	/**
	 * 
	* @Title selectUnAllotion
	* @Description 查询未分配的
	* @param map
	* @return  
	* @throws
	 */
	@RequestMapping("/selectUnAllotion")
	@CrossOrigin
	public Result selectUnAllotion(Map<String, Object> map){
		DevSupplyImeiDO supplyImei = new DevSupplyImeiDO();
		Object imei = map.get("imei");
		supplyImei.setAllotion(0);
		supplyImei.setImei(imei == null ? null : imei.toString());
		List<DevSupplyImeiDO> resList = supplyImeiService.selectPage(supplyImei);
		
		return Result.ok(resList);
	}
}
